<!DOCTYPE html>
<html>
<head>
    <:include file="../common/css.html" title="管理"/>
</head>
<body>
<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 表格工具栏 -->
            <form class="layui-form toolbar">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">评论内容:</label>
                        <div class="layui-input-inline">
                            <input name="name" id="remarkContent" class="layui-input" placeholder="输入内容"/>
                        </div>
                    </div>
                    <div class="layui-inline">&emsp;
                        <button class="layui-btn icon-btn" lay-filter="courseTbSearch" lay-submit>
                            <i class="layui-icon">&#xe615;</i>搜索
                        </button>
                    </div>
                </div>
            </form>
            <!-- 数据表格 -->
            <table id="courseTable" lay-filter="courseTable"></table>
        </div>
    </div>
</div>
<!-- 表格操作列 -->
<script type="text/html" id="courseTbBar">
<!--    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>-->
<!--    <a class="layui-btn layui-btn layui-btn-xs" lay-event="video">添加视频</a>-->
    <a class="layui-btn layui-btn-xs" lay-event="shenhe">审核</a>

<!--    <a class="layui-btn layui-btn-danger layui-btn-xs"-->
<!--       data-dropdown="#courseTbDelDrop{{d.LAY_INDEX}}" no-shade="true">删除</a>-->
<!--    <div class="dropdown-menu-nav dropdown-popconfirm dropdown-top-right layui-hide"-->
<!--         id="courseTbDelDrop{{d.LAY_INDEX}}"-->
<!--         style="max-width: 200px;white-space: normal;min-width: auto;margin-left: 10px;">-->
<!--        <div class="dropdown-anchor"></div>-->
<!--        <div class="dropdown-popconfirm-title">-->
<!--            <i class="layui-icon layui-icon-help"></i>-->
<!--            确定要删除吗？-->
<!--        </div>-->
<!--        <div class="dropdown-popconfirm-btn">-->
<!--            <a class="layui-btn" btn-cancel>取消</a>-->
<!--            <a class="layui-btn layui-btn-normal" lay-event="del">确定</a>-->
<!--        </div>-->
<!--    </div>-->

</script>
<script type="text/html" id="shEditDialog">
    <form id="shEditForm" lay-filter="shEditForm" class="layui-form model-form">
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">审核:</label>
            <div class="layui-input-block">
                <input type="radio" name="state" value="1" title="审核通过" checked="">
                <input type="radio" name="state" value="2" title="不通过">
            </div>
        </div>
        <div class="layui-form-item text-right">
            <button class="layui-btn" lay-filter="shEditSubmit" lay-submit>保存</button>
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
        </div>
    </form>
</script>


<!-- 表单弹窗-->
<script type="text/html" id="courseEditDialog">
    <form id="courseEditForm" lay-filter="courseEditForm" class="layui-form model-form">
        <input name="id" type="hidden"/>
<!--        <div class="layui-form-item">-->
<!--            <label class="layui-form-label layui-form-required">回复评论:</label>-->
<!--        </div>-->
<!--        <div class="layui-form-item">-->
<!--            <label class="layui-form-label layui-form-required">图片:</label>-->
<!--            <div class="layui-input-block" style="width:300px;float: left;margin-left: 0;">-->
<!--                <input name="image" placeholder="请输入图片" class="layui-input"-->
<!--                       lay-verify="required" required readonly/>-->
<!--            </div>-->
<!--            <div class="layui-input-inline layui-btn-container" style="width:auto;">-->
<!--			   <span type="button" class="layui-btn layui-btn-primary" id="LAY_image_upload">-->
<!--				<i class="layui-icon">&#xe67c;</i>上传图片-->
<!--			   </span>-->
<!--		    </div>-->
<!--        </div>-->
<!--        <div class="layui-form-item">-->
<!--            <label class="layui-form-label layui-form-required">类别:</label>-->
<!--            <div class="layui-input-block">-->
<!--                <input type="radio" name="type" value="free" title="免费课程" checked="">-->
<!--                <input type="radio" name="type" value="audition" title="试听课程">-->
<!--            </div>-->
<!--        </div>-->
<!--        <div class="layui-form-item">-->
<!--            <label class="layui-form-label layui-form-required">第几条评论:</label>-->
<!--            <div class="layui-input-block">-->
<!--                <select name="type" lay-verType="tips" lay-verify="required" required>-->
<!--                    <option value="">请选择</option>-->
<!--                    <:for items="${remarkList}" var="item">-->
<!--                        <option>${item.id}</option>-->
<!--                    </:for>-->
<!--                </select>-->
<!--            </div>-->

<!--        </div>-->

<!--        <div class="layui-form-item">-->
<!--            <label class="layui-form-label layui-form-required">回复的评论:</label>-->
<!--            <div class="layui-input-block">-->
<!--                 <select name="type" lay-verType="tips" lay-verify="required" required>-->
<!--                      <option value="">请选择</option>-->
<!--                      <:for items="${remarkList}" var="item">-->
<!--                           <option value="${item.id}">${item.content}</option>-->
<!--                      </:for>-->
<!--                 </select>-->
<!--            </div>-->

<!--        </div>-->

        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">内容:</label>
            <div class="layui-input-block">
                <textarea name="content" id="eContentEdt" style="display: none;"></textarea>
            </div>
        </div>
        <div class="layui-form-item text-right">
            <button class="layui-btn" lay-filter="courseEditSubmit" lay-submit>保存</button>
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
        </div>
    </form>
</script>


<:include file="../common/js.html"/>
<script>
    layui.use(['layer', 'form', 'table', 'tableX', 'util', 'admin', 'dropdown', 'layedit','upload'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var tableX = layui.tableX;
        var util = layui.util;
        var admin = layui.admin;
        var layedit = layui.layedit;

        /* 渲染表格 */
        var insTb = tableX.render({
            elem: '#courseTable',
            url: 'release-remark/page',
            page: true,
            toolbar: ['<p>',
                '<button lay-event="add" class="layui-btn layui-btn-sm icon-btn"><i class="layui-icon">&#xe654;</i>回复评论</button>&nbsp;',
                '<button lay-event="del" class="layui-btn layui-btn-sm layui-btn-danger icon-btn"><i class="layui-icon">&#xe640;</i>删除</button>',
                '</p>'].join(''),
            cellMinWidth: 100,
            cols: [[
                {type: 'checkbox'},
                {type: 'numbers'},
                {field: 'userId', title: '作者', templet:user,sort: true},
                {field: 'releaseId', title: '发布', templet:getArticle,sort: true},
                {field: 'content', title: '内容', sort: true},
                {field: 'start', title: '审核状态',templet:state,sort: true},
                {field: 'parentId', title: '回复帖子',templet:back,sort: true},
                {
                    field: 'createTime', title: '创建时间', templet: function (d) {
                        return util.toDateString(d.createTime);
                    }, sort: true
                },
                {title: '操作', toolbar: '#courseTbBar', align: 'center', width: 180, minWidth: 180}
            ]]
        });

        function state(d){
            if(d.start == 0){
                return "未审核";
            }else if(d.start == 1){
                return "审核通过";
            }else if(d.start == 2){
                return "审核不通过";
            }
        }

        function back(d){
            var content = $.ajax({url:"/release-remark/getContent",data:{id:d.parentId},async:false});
            return content.responseText;
        }

        function user(d){
        	var username = $.ajax({url:"/users/userId",data:{userId:d.userId},async:false});
        	return username.responseText;
        }

        
        function getArticle(d){
            var typename = $.ajax({url:"/course/release/getNameById",data:{id:d.releaseId},async:false});
            return typename.responseText;
        }
        
        /* 表格搜索 */
        form.on('submit(courseTbSearch)', function (data) {
            insTb.reload({where: data.field, page: {curr: 1}});
            return false;
        });

        // // 搜索评论
        // form.on('submit(courseTbSearch)', function (data) {
        //     var content = layedit.build('remarkContent'); // 建立编辑器
        //     data.field.content = layedit.getContent(content);
        //
        //     var loadIndex = layer.load(2);
        //     $.post('article-remark/update', data.field, function (res) {
        //         layer.close(loadIndex);
        //         if (0 === res.code) {
        //             layer.close(dIndex);
        //             layer.msg(res.msg, {icon: 1});
        //             insTb.reload({page: {curr: 1}});
        //         } else {
        //             layer.msg(res.msg, {icon: 2, anim: 6});
        //         }
        //     });
        //     return false;
        // });




        /* 表格工具条点击事件 */
        table.on('tool(courseTable)', function (obj) {
            if ('edit' === obj.event) { // 修改
                showEditModel(obj.data);
            } else if ('del' === obj.event) { // 删除
                doDel(obj);
            }else if('shenhe' === obj.event){
                shEditModel(obj.data);
            }
        });

        /* 表格头工具栏点击事件 */
        table.on('toolbar(courseTable)', function (obj) {
            if ('add' === obj.event) { // 回复
                //showEditModel();
                var checkRows = table.checkStatus('courseTable');
                if (0 === checkRows.data.length) {
                    return layer.msg('请选择要回复的评论', {icon: 2, anim: 6});
                }else if(1 == checkRows.data.length){
                    var ids = checkRows.data.map(function (d) {
                        return d.id;
                    });
                    backRemark({ids: ids});
                }else {
                    return layer.msg('一次只能回复一个评论！', {icon: 2, anim: 6});
                }


            } else if ('del' === obj.event) { // 删除
                var checkRows = table.checkStatus('courseTable');
                if (0 === checkRows.data.length) {
                    return layer.msg('请选择要删除的数据', {icon: 2, anim: 6});
                }
                var ids = checkRows.data.map(function (d) {
                    return d.id;
                });
                doDel({ids: ids});
            }
        });



        /* 显示表单弹窗 不使用 */
        function showEditModel(mData) {
            admin.open({
                type: 1,
                area: ['1000px','600px'],
                title: (mData ? '修改' : '添加') + '',
                content: $('#courseEditDialog').html(),
                success: function (layero, dIndex) {
                	if(mData){
                		$('#eContentEdt').html(mData.content);
                	}
                	
                	
                	// layedit.set({
              		//   uploadImage: {
              		//     url: '/article/edit_upload' //更新图片接口url
              		//   }
	              	// });


	              	var editIndex = layedit.build('eContentEdt'); // 建立编辑器
	              	
	              	// layui.upload.render({
                    //     elem: '#LAY_image_upload'
                    //     , url: '/article/upload'
                    //     , accept: 'file' //指定允许上传时校验的文件类型
                    //     , exts: 'jpg|png'
              		//      , size: 102400 //最大允许上传的文件大小
                    //     , before: function (obj) {
                    //   	  layer.msg('正在上传中，请稍等...', {icon: 16,offset: 't',shade:0.3,offset: '170px'});
                    //     }
                    //     , done: function (res) {
                    //         if (res.code == 0) {
                    //       	  layer.msg(res.msg, {anim: 6,icon: 6,offset: 't',shade:0.3,offset: '30px',time: 1500}, function(index){
              		// 				//给模板url赋值
              		// 				$("input[name='image']").val("http://localhost:8081/getFileURL/file/"+res.url);
              		// 			});
                    //         } else {
                    //       	  layer.msg(res.msg, {anim: 6,icon: 5,offset: 't',shade: 0.3,offset: '30px',time: 1500});
                    //         }
                    //     }
                    // });
	              	
                    // 回显表单数据
                    form.val('courseEditForm', mData);
                    
                    // 表单提交事件
                    form.on('submit(courseEditSubmit)', function (data) {
                    	data.field.content = layedit.getContent(editIndex);
                        console.log(data.field);
                        var loadIndex = layer.load(2);
                        $.post(mData ? 'article-remark/update' : 'article-remark/save', data.field, function (res) {
                            layer.close(loadIndex);
                            if (0 === res.code) {
                                layer.close(dIndex);
                                layer.msg(res.msg, {icon: 1});
                                insTb.reload({page: {curr: 1}});
                            } else {
                                layer.msg(res.msg, {icon: 2, anim: 6});
                            }
                        });
                        return false;
                    });
                }
            });
        }

        /* 显示表单弹窗 */
        function backRemark(obj) {
            admin.open({
                type: 1,
                area: ['1000px','600px'],
                title: '回复',
                content: $('#courseEditDialog').html(),
                success: function (layero, dIndex) {
                    var remarkId = obj;
                    if(obj){
                        $('#eContentEdt').html(obj.content);
                    }

                    var editIndex = layedit.build('eContentEdt'); // 建立编辑器



                    // 回显表单数据
                    form.val('courseEditForm', obj);

                    // 表单提交事件
                    form.on('submit(courseEditSubmit)', function (data) {
                        data.field.content = layedit.getContent(editIndex);
                        console.log(data.field);
                        console.log(remarkId);

                        if (remarkId.ids) {
                                var loadIndex = layer.load(2);
                                admin.req('release-remark/saveBatch', {ids:JSON.stringify(obj.ids),content:data.field.content}, function (res) {
                                    layer.close(loadIndex);
                                    if (0 === res.code) {
                                        layer.close(dIndex);
                                        layer.msg(res.msg, {icon: 1});
                                        insTb.reload({page: {curr: 1}});
                                    } else {
                                        layer.msg(res.msg, {icon: 2, anim: 6});
                                    }
                                }, 'post');
                        } else {  //
                            var loadIndex = layer.load(2);
                            $.get('release-remark/save', {id: obj.data.id,content:data.field.content}, function (res) {
                                layer.close(loadIndex);
                                if (0 === res.code) {
                                    layer.close(dIndex);
                                    layer.msg(res.msg, {icon: 1});
                                    insTb.reload({page: {curr: 1}});
                                } else {
                                    layer.msg(res.msg, {icon: 2, anim: 6});
                                }
                            });
                        }
                        return false;
                    });
                }
            });
        }


        /* 显示表单弹窗 审核 */
        function shEditModel(mData) {
            admin.open({
                type: 1,
                title: '审核',
                content: $('#shEditDialog').html(),
                success: function (layero, dIndex) {
                    form.render();
                    // 表单提交事件
                    form.on('submit(shEditSubmit)', function (data) {
                        var loadIndex = layer.load(2);
                        $.post('release-remark/shenhe', {id:mData.id,state:data.field.state,userId:mData.userId}, function (res) {
                            layer.close(loadIndex);
                            if (0 === res.code) {
                                layer.close(dIndex);
                                layer.msg(res.msg, {icon: 1});
                                insTb.reload({page: {curr: 1}});
                            } else {
                                layer.msg(res.msg, {icon: 2, anim: 6});
                            }
                        });
                        return false;
                    });
                }
            });
        }

        /* 删除 */
        function doDel(obj) {
            if (obj.ids) {  // 批量删除
                admin.confirm('确定要删除选中数据吗？', function (i) {
                    layer.close(i);
                    var loadIndex = layer.load(2);
                    admin.req('release-remark/removeBatch', JSON.stringify(obj.ids), function (res) {
                        layer.close(loadIndex);
                        if (0 === res.code) {
                            layer.msg(res.msg, {icon: 1});
                            insTb.reload({page: {curr: 1}});
                        } else {
                            layer.msg(res.msg, {icon: 2, anim: 6});
                        }
                    }, 'post');
                });
            } else {  // 单个删除
                var loadIndex = layer.load(2);
                $.get('release-remark/delete', {id: obj.data.id}, function (res) {
                    layer.close(loadIndex);
                    if (0 === res.code) {
                        layer.msg(res.msg, {icon: 1});
                        insTb.reload({page: {curr: 1}});
                    } else {
                        layer.msg(res.msg, {icon: 2, anim: 6});
                    }
                });
            }

        }

    });
</script>
</body>
</html>